using System;
using System.Collections.Generic;
using System.Drawing;

namespace HuaRongDaoGame.Models
{
    /// <summary>
    /// 华容道游戏布局类
    /// </summary>
    public class GameLayout
    {
        /// <summary>
        /// 布局名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 布局描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 难度等级（1-简单，2-中等，3-困难）
        /// </summary>
        public int Difficulty { get; set; }

        /// <summary>
        /// 布局中的方块定义
        /// </summary>
        public List<BlockDefinition> BlockDefinitions { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public GameLayout(string name, string description, int difficulty)
        {
            Name = name;
            Description = description;
            Difficulty = difficulty;
            BlockDefinitions = new List<BlockDefinition>();
        }

        /// <summary>
        /// 添加方块定义
        /// </summary>
        public void AddBlockDefinition(BlockDefinition blockDef)
        {
            BlockDefinitions.Add(blockDef);
        }

        /// <summary>
        /// 创建游戏棋盘
        /// </summary>
        public GameBoard CreateGameBoard()
        {
            var gameBoard = new GameBoard();
            
            foreach (var blockDef in BlockDefinitions)
            {
                var block = new Block(
                    blockDef.Id,
                    blockDef.Name,
                    blockDef.Width,
                    blockDef.Height,
                    blockDef.X,
                    blockDef.Y,
                    blockDef.Color,
                    blockDef.IsCaoCao
                );
                
                gameBoard.AddBlock(block);
            }
            
            return gameBoard;
        }

        /// <summary>
        /// 获取难度描述
        /// </summary>
        public string GetDifficultyDescription()
        {
            switch (Difficulty)
            {
                case 1: return "简单";
                case 2: return "中等";
                case 3: return "困难";
                default: return "未知";
            }
        }
    }

    /// <summary>
    /// 方块定义
    /// </summary>
    public class BlockDefinition
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public Color Color { get; set; }
        public bool IsCaoCao { get; set; }

        public BlockDefinition(int id, string name, int width, int height, int x, int y, Color color, bool isCaoCao = false)
        {
            Id = id;
            Name = name;
            Width = width;
            Height = height;
            X = x;
            Y = y;
            Color = color;
            IsCaoCao = isCaoCao;
        }
    }

    /// <summary>
    /// 经典布局管理器
    /// </summary>
    public static class ClassicLayouts
    {
        /// <summary>
        /// 获取所有经典布局
        /// </summary>
        public static List<GameLayout> GetAllLayouts()
        {
            var layouts = new List<GameLayout>();
            
            layouts.Add(CreateHengDaoLiMa());
            layouts.Add(CreateJiangYongCaoYing());
            layouts.Add(CreateBingFenSanLu());
            layouts.Add(CreateQiXingJuZhen());
            layouts.Add(CreateBaZhenTu());
            
            return layouts;
        }

        /// <summary>
        /// 横刀立马布局
        /// </summary>
        public static GameLayout CreateHengDaoLiMa()
        {
            var layout = new GameLayout("横刀立马", "最经典的华容道布局，曹操被关羽、张飞、赵云、马超四将包围", 1);
            
            // 曹操（2x2）
            layout.AddBlockDefinition(new BlockDefinition(1, "曹操", 2, 2, 1, 1, Color.Red, true));
            
            // 关羽（2x1）
            layout.AddBlockDefinition(new BlockDefinition(2, "关羽", 2, 1, 1, 0, Color.Green));
            
            // 张飞（1x2）
            layout.AddBlockDefinition(new BlockDefinition(3, "张飞", 1, 2, 0, 1, Color.Blue));
            
            // 赵云（1x2）
            layout.AddBlockDefinition(new BlockDefinition(4, "赵云", 1, 2, 3, 1, Color.Yellow));
            
            // 马超（1x2）
            layout.AddBlockDefinition(new BlockDefinition(5, "马超", 1, 2, 0, 3, Color.Orange));
            
            // 黄忠（1x2）
            layout.AddBlockDefinition(new BlockDefinition(6, "黄忠", 1, 2, 3, 3, Color.Purple));
            
            // 兵卒（1x1）
            layout.AddBlockDefinition(new BlockDefinition(7, "兵卒1", 1, 1, 0, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(8, "兵卒2", 1, 1, 3, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(9, "兵卒3", 1, 1, 1, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(10, "兵卒4", 1, 1, 2, 3, Color.Gray));
            
            return layout;
        }

        /// <summary>
        /// 将拥曹营布局
        /// </summary>
        public static GameLayout CreateJiangYongCaoYing()
        {
            var layout = new GameLayout("将拥曹营", "曹操被四将紧密包围，需要巧妙移动才能脱身", 2);
            
            // 曹操（2x2）
            layout.AddBlockDefinition(new BlockDefinition(1, "曹操", 2, 2, 1, 1, Color.Red, true));
            
            // 四将（1x2）
            layout.AddBlockDefinition(new BlockDefinition(2, "关羽", 1, 2, 0, 1, Color.Green));
            layout.AddBlockDefinition(new BlockDefinition(3, "张飞", 1, 2, 3, 1, Color.Blue));
            layout.AddBlockDefinition(new BlockDefinition(4, "赵云", 1, 2, 1, 0, Color.Yellow));
            layout.AddBlockDefinition(new BlockDefinition(5, "马超", 1, 2, 1, 3, Color.Orange));
            
            // 兵卒（1x1）
            layout.AddBlockDefinition(new BlockDefinition(6, "兵卒1", 1, 1, 0, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(7, "兵卒2", 1, 1, 3, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(8, "兵卒3", 1, 1, 0, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(9, "兵卒4", 1, 1, 3, 3, Color.Gray));
            
            return layout;
        }

        /// <summary>
        /// 兵分三路布局
        /// </summary>
        public static GameLayout CreateBingFenSanLu()
        {
            var layout = new GameLayout("兵分三路", "兵卒分成三路，需要合理调度才能让曹操脱身", 2);
            
            // 曹操（2x2）
            layout.AddBlockDefinition(new BlockDefinition(1, "曹操", 2, 2, 1, 1, Color.Red, true));
            
            // 大将（2x1）
            layout.AddBlockDefinition(new BlockDefinition(2, "关羽", 2, 1, 1, 0, Color.Green));
            
            // 中军（1x2）
            layout.AddBlockDefinition(new BlockDefinition(3, "张飞", 1, 2, 0, 1, Color.Blue));
            layout.AddBlockDefinition(new BlockDefinition(4, "赵云", 1, 2, 3, 1, Color.Yellow));
            
            // 兵卒（1x1）
            layout.AddBlockDefinition(new BlockDefinition(5, "兵卒1", 1, 1, 0, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(6, "兵卒2", 1, 1, 3, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(7, "兵卒3", 1, 1, 0, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(8, "兵卒4", 1, 1, 3, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(9, "兵卒5", 1, 1, 1, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(10, "兵卒6", 1, 1, 2, 3, Color.Gray));
            
            return layout;
        }

        /// <summary>
        /// 七星聚阵布局
        /// </summary>
        public static GameLayout CreateQiXingJuZhen()
        {
            var layout = new GameLayout("七星聚阵", "七个兵卒呈七星阵势，需要精确计算移动顺序", 3);
            
            // 曹操（2x2）
            layout.AddBlockDefinition(new BlockDefinition(1, "曹操", 2, 2, 1, 1, Color.Red, true));
            
            // 大将（1x2）
            layout.AddBlockDefinition(new BlockDefinition(2, "关羽", 1, 2, 0, 1, Color.Green));
            layout.AddBlockDefinition(new BlockDefinition(3, "张飞", 1, 2, 3, 1, Color.Blue));
            
            // 兵卒（1x1）
            layout.AddBlockDefinition(new BlockDefinition(4, "兵卒1", 1, 1, 0, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(5, "兵卒2", 1, 1, 3, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(6, "兵卒3", 1, 1, 0, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(7, "兵卒4", 1, 1, 3, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(8, "兵卒5", 1, 1, 1, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(9, "兵卒6", 1, 1, 2, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(10, "兵卒7", 1, 1, 1, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(11, "兵卒8", 1, 1, 2, 3, Color.Gray));
            
            return layout;
        }

        /// <summary>
        /// 八卦阵布局
        /// </summary>
        public static GameLayout CreateBaZhenTu()
        {
            var layout = new GameLayout("八卦阵", "最复杂的布局之一，需要极高的技巧才能完成", 3);
            
            // 曹操（2x2）
            layout.AddBlockDefinition(new BlockDefinition(1, "曹操", 2, 2, 1, 1, Color.Red, true));
            
            // 四将（1x1）
            layout.AddBlockDefinition(new BlockDefinition(2, "关羽", 1, 1, 0, 0, Color.Green));
            layout.AddBlockDefinition(new BlockDefinition(3, "张飞", 1, 1, 3, 0, Color.Blue));
            layout.AddBlockDefinition(new BlockDefinition(4, "赵云", 1, 1, 0, 3, Color.Yellow));
            layout.AddBlockDefinition(new BlockDefinition(5, "马超", 1, 1, 3, 3, Color.Orange));
            
            // 兵卒（1x1）
            layout.AddBlockDefinition(new BlockDefinition(6, "兵卒1", 1, 1, 1, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(7, "兵卒2", 1, 1, 2, 0, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(8, "兵卒3", 1, 1, 0, 1, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(9, "兵卒4", 1, 1, 3, 1, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(10, "兵卒5", 1, 1, 0, 2, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(11, "兵卒6", 1, 1, 3, 2, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(12, "兵卒7", 1, 1, 1, 3, Color.Gray));
            layout.AddBlockDefinition(new BlockDefinition(13, "兵卒8", 1, 1, 2, 3, Color.Gray));
            
            return layout;
        }

        /// <summary>
        /// 根据名称获取布局
        /// </summary>
        public static GameLayout GetLayoutByName(string name)
        {
            var layouts = GetAllLayouts();
            return layouts.Find(l => l.Name == name);
        }

        /// <summary>
        /// 根据难度获取布局
        /// </summary>
        public static List<GameLayout> GetLayoutsByDifficulty(int difficulty)
        {
            var layouts = GetAllLayouts();
            return layouts.FindAll(l => l.Difficulty == difficulty);
        }
    }
} 